import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { RouteRecordRaw } from 'vue-router'
import router, { constantRoutes, asyncRoutes as dynamicRoutes } from '@/router'
import Layout from '@/layout/index.vue'
import ParentView from '@/components/ParentView/index.vue'
// import { myMenu } from '@/apis'
import Has from '@/utils/has'

// 匹配views里面所有的.vue文件
const modules = import.meta.glob('./../../views/**/*.vue')

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRoutes: RouteRecordRaw[], lastRouter: boolean | RouteRecordRaw = false, type = false) {
  return asyncRoutes.filter((route) => {
    if (type && route.children) {
      route.children = filterChildren(route.children)
    }
    if (route.component && typeof route.component === 'string') {
      // Layout ParentView 组件特殊处理
      if (route['component'] === 'Layout') {
        route['component'] = Layout as never
      } else if (route['component'] === 'ParentView') {
        route['component'] = ParentView as never
      } else {
        route['component'] = loadView(route['component']) as never
      }
    }
    if (route.children !== null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children, route, type)
    } else {
      delete route['children']
      delete route['redirect']
    }
    return true
  })
}

function filterChildren(childrenRoutes: RouteRecordRaw[], lastRouter: boolean | RouteRecordRaw = false) {
  let children: RouteRecordRaw[] = []
  childrenRoutes.forEach((el, index) => {
    if (el.children && el.children.length) {
      if (typeof el.component === 'string' && el['component'] === 'ParentView' && !lastRouter) {
        ;(el['children'] as RouteRecordRaw[]).forEach((c: RouteRecordRaw) => {
          c.path = el['path'] + '/' + c.path
          if (c.children && c.children.length) {
            children = children.concat(filterChildren(c.children, c))
            return
          }
          children.push(c)
        })
        return
      }
    }
    if (lastRouter && typeof lastRouter === 'object') {
      el.path = lastRouter.path + '/' + el.path
    }
    children = children.concat(el)
  })
  return children
}

// 动态路由遍历，验证是否具备权限
export function filterDynamicRoutes(routes: RouteRecordRaw[]) {
  const arr: RouteRecordRaw[] = []
  routes.forEach((route) => {
    if (route.meta?.permissions) {
      if (Has.hasPermOr(route.meta?.permissions)) {
        arr.push(route)
      }
    } else if (route.meta?.roles) {
      if (Has.hasRoleOr(route.meta?.roles)) {
        arr.push(route)
      }
    }
  })
  return arr
}

// 加载模块
export const loadView = (view: string) => {
  let res
  for (const path in modules) {
    const dir = path.split('views/')[1].split('.vue')[0]
    if (dir === view) {
      res = () => modules[path]()
    }
  }
  return res
}
interface ObjectWithChildren {
  id: number
  name: string
  children?: ObjectWithChildren[]
  extend: any
}
const mergeObjectsByParent = (objects: ObjectWithChildren[]): ObjectWithChildren[] => {
  const mergedObjects: { [key: string]: ObjectWithChildren } = {}
  objects.forEach((obj) => {
    const { name, children } = obj
    const extend = typeof obj.extend === 'string' ? JSON.parse(obj.extend) : obj.extend
    const key = name // 以 name 作为父级的唯一标识
    if (!mergedObjects[key]) {
      mergedObjects[key] = { ...obj }
    } else {
      mergedObjects[key].children = (mergedObjects[key].children || []).concat(children || [])
      mergedObjects[key].extend = { ...extend }
    }
  })

  return Object.values(mergedObjects)
}

const storeSetup = () => {
  const routes = ref<RouteRecordRaw[]>([]) // 常驻路由 + 动态路由（这时候component已经从字符串转为模块）
  const topbarRoutes = ref<RouteRecordRaw[]>([]) // 页签路由
  const sidebarRoutes = ref<RouteRecordRaw[]>([]) // 侧边栏路由

  const setRoutes = (routesArray: RouteRecordRaw[]) => {
    routes.value = constantRoutes.concat(routesArray)
  }

  const setTopbarRoutes = (routes: RouteRecordRaw[]) => {
    topbarRoutes.value = routes
  }

  const setSidebarRoutes = (routes: RouteRecordRaw[]) => {
    sidebarRoutes.value = routes
  }

  // 路由格式化
  const formatRouter = (arr: any) => {
    const res = arr.map((item: any) => {
      if (typeof item.extend === 'string') {
        item.extend = JSON.parse(item.extend)
      }
      return {
        path: item.extend.path || item.uri,
        name: item.name,
        redirect: item.extend.redirect,
        component: item.extend.component,
        children: item.children && item.children.length ? formatRouter(item.children) : undefined,
        meta: {
          hidden: item.extend.hidden,
          title: item.extend.title,
          svgIcon: item.extend.svgIcon,
          icon: item.icon || item.extend.icon,
          keepAlive: item.extend.keepAlive,
          affix: item.extend.affix,
          activeMenu: item.extend.activeMenu,
          bold: item.extend.bold
        }
      }
    })
    return res
  }

  /** 生成路由 */
  const generateRoutes = (): Promise<RouteRecordRaw[]> => {
    return new Promise((resolve) => {
      // 向后端请求路由数据
      // myMenu().then((res) => {
      //   const mergedObjects = mergeObjectsByParent(res.data)
      //   const routers = formatRouter(mergedObjects)
      //   const sdata = JSON.parse(JSON.stringify(routers))
      //   const rdata = JSON.parse(JSON.stringify(routers))
      //   const defaultData = JSON.parse(JSON.stringify(routers))
      //   const sidebarRoutes = filterAsyncRouter(sdata)
      //   const rewriteRoutes = filterAsyncRouter(rdata, false, true)
      //   const defaultRoutes = filterAsyncRouter(defaultData)
      //   const asyncRoutes = filterDynamicRoutes(dynamicRoutes)
      //   asyncRoutes.forEach((route) => {
      //     router.addRoute(route)
      //   })
      //   setRoutes(rewriteRoutes)
      //   setSidebarRoutes(constantRoutes.concat(sidebarRoutes))
      //   setTopbarRoutes(defaultRoutes)
      //   resolve(rewriteRoutes)
      // })
      const mergedObjects = mergeObjectsByParent([])
      const routers = formatRouter(mergedObjects)
      const sdata = JSON.parse(JSON.stringify(routers))
      const rdata = JSON.parse(JSON.stringify(routers))
      const defaultData = JSON.parse(JSON.stringify(routers))
      const sidebarRoutes = filterAsyncRouter(sdata)
      const rewriteRoutes = filterAsyncRouter(rdata, false, true)
      const defaultRoutes = filterAsyncRouter(defaultData)
      const asyncRoutes = filterDynamicRoutes(dynamicRoutes)
      asyncRoutes.forEach((route) => {
        router.addRoute(route)
      })
      setRoutes(rewriteRoutes)
      setSidebarRoutes(constantRoutes.concat(sidebarRoutes))
      setTopbarRoutes(defaultRoutes)
      resolve(rewriteRoutes)
    })
  }

  return {
    routes,
    topbarRoutes,
    sidebarRoutes,
    generateRoutes
  }
}

export const usePermissionStore = defineStore('permission', storeSetup, { persist: true })
